﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Com.Mapabc.Maps.Api.BaseTypes;
using System.Collections.ObjectModel;
using Com.Mapabc.Maps.Api.Overlays;
using Com.Mapabc.Maps.Api;
using System.Collections;


namespace MapABCAPI_WP7.Samples
{
    public partial class MapTool : PhoneApplicationPage
    {
        public MapTool()
        {
            InitializeComponent();
            map.Tap += new EventHandler<GestureEventArgs>(map_Tap);
        }

        void map_Tap(object sender, GestureEventArgs e)
        {
           Point p = e.GetPosition(map);
           Pixel.Text = p.X.ToString() +","+ p.Y.ToString();
           
        }
        /// <summary>
        /// 在指定位置添加点对象
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void addLocationMarker(object sender, GestureEventArgs e)
        {
            Hidden();
            textBlock1.Visibility = System.Windows.Visibility.Visible;
            LngX.Visibility = System.Windows.Visibility.Visible;
            textBlock2.Visibility = System.Windows.Visibility.Visible;
            LatY.Visibility = System.Windows.Visibility.Visible;
            button1.Visibility = System.Windows.Visibility.Visible;
            Pivot.SelectedIndex = 1;//显示效果
        }
        private void addMarker(object sender, RoutedEventArgs e)
        {
            map.Children.Clear();//清除地图上的所有子容器
            MMarker mk = new MMarker(new MLngLat(double.Parse(LngX.Text), double.Parse(LatY.Text)))//构造MMarker
            {
                IconURL = "http://code.mapabc.com/images/lan_1.png",
                CanShowTip = false,
            };
            map.Children.Add(mk);//添加到地图
            map.Center = mk.LngLat;//设置地图中心点
            MOverlay[] overlays = new MOverlay[] { mk };
            map.SetFitview(overlays);
        }
        /// <summary>
        /// 计算多点距离
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CalculateDistance(object sender, GestureEventArgs e)
        {
            Hidden();
            textBlock3.Visibility = System.Windows.Visibility.Visible;
            CoorsXY.Visibility = System.Windows.Visibility.Visible;
            textBlock4.Visibility = System.Windows.Visibility.Visible;
            Result.Visibility = System.Windows.Visibility.Visible;
            button2.Visibility = System.Windows.Visibility.Visible;
            Pivot.SelectedIndex = 1;//显示效果
        }
        private void Calculate(object sender, RoutedEventArgs e)
        {
            map.Children.Clear();//清除地图上的所有子容器
            MLngLatCollection mc = new MLngLatCollection();//构造经纬度序列集合
            String Coors = CoorsXY.Text;
            String[] XY = Coors.Split(';');
            int CoorsLength = XY.Length;
            if (Coors.EndsWith(";"))
            {
                CoorsLength = CoorsLength - 1;
            }
            for (int i = 0; i < CoorsLength; i++)
            {
                String[] str = XY[i].Split(',');
                mc.Add(new MLngLat(double.Parse(str[0]), double.Parse(str[1])));
            }
            double[] numbers = map.CalculateDistance(mc);
            String resultStr = "";
            for (int j = 0; j < numbers.Length; j++)
            {
                resultStr = resultStr + numbers[j] + ",";
            }
            Result.Text = resultStr;
            MPolyline polyline = new MPolyline();
            polyline.LngLats = mc;//mc为组成线的经纬度坐标串
            polyline.LineColor = Utilities.HexToColor("#022672");//线的颜色
            polyline.LineThickness = 3;//线的粗细
            polyline.CanShowTip = false;
            map.Children.Add(polyline);//添加到地图
            MOverlay[] overlays = new MOverlay[] { polyline };
            map.SetFitview(overlays);
        }
        /// <summary>
        /// 经纬度坐标和容器像素坐标互换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LngLatToContainerPixel(object sender, GestureEventArgs e)
        {
            Hidden();
            textBlock5.Visibility = System.Windows.Visibility.Visible;
            Pixel.Visibility = System.Windows.Visibility.Visible;
            button3.Visibility = System.Windows.Visibility.Visible;
            textBlock6.Visibility = System.Windows.Visibility.Visible;
            LngLat.Visibility = System.Windows.Visibility.Visible;
            Pivot.SelectedIndex = 1;//显示效果
        }

        private void FromScreenPixelToLngLat(object sender, RoutedEventArgs e)
        {
            try{
            String a = Pixel.Text;
            String[] XY = a.Split (',');         
            MLngLat lnglat = map.FromScreenPixelToLngLat(new Point(int.Parse(XY[0]), int.Parse(XY[1])));
            LngLat.Text = lnglat.LngX.ToString() + "," + lnglat.LatY.ToString();
            }
            catch (Exception exception)
            {
                MessageBox.Show("请输入正确的屏幕坐标!");
            }
        }

        private void Hidden() 
        {
            textBlock1.Visibility = System.Windows.Visibility.Collapsed;
            LngX.Visibility = System.Windows.Visibility.Collapsed;
            textBlock2.Visibility = System.Windows.Visibility.Collapsed;
            LatY.Visibility = System.Windows.Visibility.Collapsed;
            button1.Visibility = System.Windows.Visibility.Collapsed;
            textBlock3.Visibility = System.Windows.Visibility.Collapsed;
            CoorsXY.Visibility = System.Windows.Visibility.Collapsed;
            textBlock4.Visibility = System.Windows.Visibility.Collapsed;
            Result.Visibility = System.Windows.Visibility.Collapsed;
            button2.Visibility = System.Windows.Visibility.Collapsed;
            textBlock5.Visibility = System.Windows.Visibility.Collapsed;
            Pixel.Visibility = System.Windows.Visibility.Collapsed;
            button3.Visibility = System.Windows.Visibility.Collapsed;
            textBlock6.Visibility = System.Windows.Visibility.Collapsed;
            LngLat.Visibility = System.Windows.Visibility.Collapsed;
            

        }
        /// <summary>
        /// 重写后退键
        /// </summary>
        /// <param name="e"></param>
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (Pivot.SelectedIndex == 1)
            {
                e.Cancel = true;
                Pivot.SelectedIndex = 0;
            }
        }

       

        

      
    }
}